package com.servlet.yanglaoyuan.controller;

import com.servlet.yanglaoyuan.common.Result;
import com.servlet.yanglaoyuan.pojo.Health_Checkup_Data;
import com.servlet.yanglaoyuan.service.Health_Checkup_DataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
public class health_checkup_dateController {
    @Autowired
    private Health_Checkup_DataService health_checkup_dataService;

    @PostMapping("/checkData")
    public Result  add(@RequestBody Health_Checkup_Data record) {
        log.info("添加检查数据: {}",record);

        LocalDateTime date = record.getDate();
        if (date != null) {
            LocalDateTime tomorrow = date.plusDays(1);
            log.info("接收到的时间: {}", date);
            log.info("明天的时间: {}", tomorrow);
            date = tomorrow;
            // 可以在此进行时间比较或其他业务逻辑处理
        } else {
            log.warn("时间字段为空");
        }
        record.setDate(date);
        health_checkup_dataService.add(record);
        return Result.success();
    }

    @GetMapping("/checkData/{id}")
    public Result findAllByElderlyId(@PathVariable  Integer id) {
        log.info("获取Id为："+id +"的所有检查数据");
        return Result.success(health_checkup_dataService.getByElderlyId(id));
    }

    @DeleteMapping("/checkData/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除Id为："+id +"的检查数据");
        health_checkup_dataService.deleteCheckupData(id);
        return Result.success();
    }

    @PutMapping("/checkData/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Health_Checkup_Data record) {
        log.info("更新Id为："+id +"的检查数据, 接收到的数据：{}", record);
        LocalDateTime date = record.getDate();
        if (date != null) {
            LocalDateTime tomorrow = date.plusDays(1);
            log.info("接收到的时间: {}", date);
            log.info("明天的时间: {}", tomorrow);
            date = tomorrow;
            // 可以在此进行时间比较或其他业务逻辑处理
        } else {
            log.warn("时间字段为空");
        }
        record.setId(id);
        record.setDate(date);
        log.info("更新后的检查数据：{}", record);
        health_checkup_dataService.updateCheckupData(record);
        return Result.success();
    }

}
